package com.reload.stt.spark.real_ime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import java.io.File;
import java.io.IOException;

/**
 * @Date: 2024/12/6 - 20:15
 * @Version: 17
 * @Author: Reloading
 * @Description:
 */
@RestController
public class OpenAiAudioController {
    @Autowired
    private OpenAiAudioService openAiAudioService;

    @PostMapping(value = "/openai/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> uploadAudioFile(@RequestParam("file") MultipartFile file) {
        // 调用 Service 层处理文件
        return openAiAudioService.processAudioFile(file);
    }

    // 将 MultipartFile 转为 File
    private File convertToFile(MultipartFile multipartFile) throws IOException {
        File tempFile = File.createTempFile("upload", multipartFile.getOriginalFilename());
        multipartFile.transferTo(tempFile);
        tempFile.deleteOnExit();
        return tempFile;
    }
}
